#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import os
from ..._util import WinRegistry
from ..._prototype import CustomAction
from ..._vm import Vm


class InstallChocolatey(CustomAction):

    def __init__(self, local_files=None):
        self._localFiles = local_files

    def run(self, action_name, s, ts, work_dir):
        vm = Vm(work_dir)
        vm.start(show=(s.verbose_level > 0))
        try:
            vm.wait_until_stable()
            vm.shutdown()
            vm.wait_until_stop()
        except BaseException:
            vm.stop()
            raise

    def get_after(self):
        return []

    def get_before(self):
        return []


class InstallZeroInstall(CustomAction):

    def run(self, action_name, s, ts, work_dir):
        pass

    def get_after(self):
        return []

    def get_before(self):
        return []


class InstallNpackd(CustomAction):

    def run(self, action_name, s, ts, work_dir):
        with work_dir.mount_image_file() as mp:
            # add runonce registry entry
            reg = WinRegistry(mp.drive_c_dir)
            self.add_runonce_command(reg, "powershell -Command \"(New-Object Net.WebClient).DownloadFile('http://npackd.dl.sourceforge.net/project/npackd/Npackd/1.26.7/Npackd-1.26.7.exe', 'C:\\NpackdSetup.exe')\" && start /wait C:\\NpackdSetup.exe /SLIENT")

        # run vm
        vm = Vm(work_dir)
        vm.start(show=(s.verbose_level > 0))
        try:
            vm.wait_until_stable()
            vm.shutdown()
            vm.wait_until_stop()
        except BaseException:
            vm.stop()
            raise

    def get_after(self):
        return []

    def get_before(self):
        return []


class InstallPortableApps(CustomAction):

    def run(self, action_name, s, ts, work_dir):
        with work_dir.mount_image_file() as mp:
            # add runonce registry entry
            reg = WinRegistry(mp.drive_c_dir)
            self.add_runonce_command(reg, "powershell -Command \"(New-Object Net.WebClient).DownloadFile('https://portableapps.com/redirect/?a=PortableApps.comPlatform&s=s&d=pa%3A%2F%2FPortableApps.comPlatform%2FPortableApps.com_Platform_Setup.exe', 'C:\\PASetup.exe')\" && start /wait C:\\PASetup.exe /SILENT /NORESTART")

        # run vm
        vm = Vm(work_dir)
        vm.start(show=(s.verbose_level > 0))
        try:
            vm.wait_until_stable()
            vm.shutdown()
            vm.wait_until_stop()
        except BaseException:
            vm.stop()
            raise

    def get_after(self):
        return []

    def get_before(self):
        return []
